package campus.aiit.ssl.service;

import campus.aiit.ssl.dao.*;
import campus.aiit.ssl.domain.model.*;
import campus.aiit.ssl.manage.custom.dao.DaoProxy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ihaokun
 * @date 2019/11/29 16:51
 */
@Service
public class LocationService {
    private SchoolZoneDao schoolZoneDao;
    private BuildingDao buildingDao;
    private RoomDao roomDao;
    private GlobalConfigDao globalConfigDao;
    private DeviceAutoStatusDao deviceAutoStatusDao;
    private DeviceDataCollectionDao deviceDataCollectionDao;
    private HistoricalInstructionDao historicalInstructionDao;
    private MessageDao messageDao;
    private AlarmDao alarmDao;

    @Autowired
    public LocationService(SchoolZoneDao schoolZoneDao,
                           BuildingDao buildingDao,
                           RoomDao roomDao,
                           DeviceAutoStatusDao deviceAutoStatusDao,
                           GlobalConfigDao globalConfigDao,
                           DeviceDataCollectionDao deviceDataCollectionDao,
                           HistoricalInstructionDao historicalInstructionDao,
                           MessageDao messageDao,
                           AlarmDao alarmDao) {
        this.schoolZoneDao = schoolZoneDao;
        this.buildingDao = buildingDao;
        this.roomDao = roomDao;
        this.deviceAutoStatusDao = deviceAutoStatusDao;
        this.globalConfigDao = globalConfigDao;
        this.deviceDataCollectionDao = deviceDataCollectionDao;
        this.historicalInstructionDao = historicalInstructionDao;
        this.messageDao = messageDao;
        this.alarmDao = alarmDao;
    }

    @PostConstruct
    public void afterConstruct() {
        DaoProxy<SchoolZoneDao> proxy = new DaoProxy<>();
        schoolZoneDao = proxy.getProxy(schoolZoneDao);
        DaoProxy<BuildingDao> proxy1 = new DaoProxy<>();
        buildingDao = proxy1.getProxy(buildingDao);
        DaoProxy<RoomDao> proxy2 = new DaoProxy<>();
        roomDao = proxy2.getProxy(roomDao);
        DaoProxy<DeviceDataCollectionDao> proxy3 = new DaoProxy<>();
        deviceDataCollectionDao = proxy3.getProxy(deviceDataCollectionDao);
        DaoProxy<HistoricalInstructionDao> proxy4 = new DaoProxy<>();
        historicalInstructionDao = proxy4.getProxy(historicalInstructionDao);
    }

    public SchoolZone[] schoolZones() {
        return schoolZoneDao.selectArray();
    }

    public int addZone(String name) {
        SchoolZone record = new SchoolZone();
        record.setName(name);
        return schoolZoneDao.insertSelective(record);
    }

    public int updateZone(SchoolZone zone) {
        return schoolZoneDao.updateByPrimaryKeySelective(zone);
    }

    public int removeZone(long id) {
        return schoolZoneDao.deleteByPrimaryKey(id);
    }

    public int addBuilding(Building building) {
        return buildingDao.insertSelective(building);
    }

    public int updateBuilding(Building building) {
        return buildingDao.updateByPrimaryKeySelective(building);
    }

    public Building[] buildings(long zoneId) {
        return buildingDao.selectArrayByForeignKey(zoneId);
    }

    public int deleteBuilding(long id) {
        return buildingDao.deleteByPrimaryKey(id);
    }

    public int addRoom(Room room) {
        return roomDao.insertSelective(room);
    }

    public int updateRoom(Room room) {
        return roomDao.updateByPrimaryKeySelective(room);
    }

    public int deleteRoom(long id) {
        return roomDao.deleteByPrimaryKey(id);
    }

    public Room[] rooms(long buildingId) {
        return roomDao.selectArrayByForeignKey(buildingId);
    }

    public Room[] rooms(long buildingId, Long floor) {
        return roomDao.selectArrayByForeignKeyFloor(buildingId, floor);
    }

    public Room[] getRooms(long blockId) {return roomDao.selectArrayByForeignKeyBlock(blockId);}

    public int floors(long id) {
        return buildingDao.selectFloorsById(id);
    }

    public int updateAutoStatus(DeviceAutoStatus record) {
        return deviceAutoStatusDao.updateByPrimaryKeySelective(record);
    }

    public List<Map<String, Object>> deviceAutoStatus(Long zoneId, Long buildingId, Long floor, Long roomId, Long addressCode) {
//        int home = (pageNum - 1) * pageSize;
//        int end = pageNum * pageSize;
        return deviceAutoStatusDao.selectArray(zoneId, buildingId, floor, roomId, addressCode);
    }

//    public int deviceAutoStatusPage(Long zoneId, Long buildingId, Long floor, Long roomId, Long addressCode) {
//        return deviceAutoStatusDao.selectDateNumber(zoneId, buildingId, floor, roomId, addressCode);
//    }

    public int updateGlobalConfig(GlobalConfig record) {
        return globalConfigDao.updateByPrimaryKeySelective(record);
    }

    public GlobalConfig[] globalConfig() {
        return globalConfigDao.selectArray();
    }

    public int addCollectLog(CollectLog collectLog) {
        return deviceDataCollectionDao.insertSelective(collectLog);
    }

    public int deleteCollectLog(long id) {
        return deviceDataCollectionDao.deleteByPrimaryKey(id);
    }

    public int deleteByTime(long day) {
        return deviceDataCollectionDao.deleteByTime(day);
    }

    public List<Map<String, Object>> collectLogs(Long typeId,Long SchoolZoneId, Long BuildingId, Long Floor, Long RoomId, Long DeviceId,
                                                 String startTime, String endTime) {
        if(typeId == null){
            return deviceDataCollectionDao.collectLogs((long) 1,SchoolZoneId, BuildingId, Floor, RoomId,
                    DeviceId, startTime,endTime);
        }
        return deviceDataCollectionDao.collectLogs(typeId,SchoolZoneId, BuildingId, Floor, RoomId,
                DeviceId, startTime,endTime);
    }

    public int addInstructLog(InstructLog instructLog) {
        return historicalInstructionDao.insertSelective(instructLog);
    }

    public int deleteInstructLog(long id) {
        return historicalInstructionDao.deleteByPrimaryKey(id);
    }

    public List<Map<String, Object>> instructLogs(Long SchoolZoneId, Long BuildingId, Long Floor, Long RoomId, Long DeviceId,
                                                  String startTime, String endTime) {
//        Integer home = (pageNum - 1) * pageSize;
//        Integer end = pageSize;
        return historicalInstructionDao.instructLogs(SchoolZoneId, BuildingId, Floor, RoomId,
                DeviceId, startTime, endTime);
    }

//    public int getTotalData(Long SchoolZoneId, Long BuildingId, Long Floor, Long RoomId, Long DeviceId,
//                            Date startTime, Date endTime) {
//        return historicalInstructionDao.getTotalData(SchoolZoneId, BuildingId, Floor, RoomId,
//                DeviceId, startTime, endTime);
//    }

    public Building[] buildingsByZoneName(String name) {
        return buildingDao.selectByZoneName(name);
    }

    public SchoolZone location(long roomId) {
        Room room = roomDao.selectByPrimaryKey(roomId);
        Building building = buildingDao.selectByPrimaryKey(room.getBuildingId());
        building.setRooms(new Room[]{room});
        SchoolZone schoolZone = schoolZoneDao.selectByPrimaryKey(building.getZoneId());
        schoolZone.setBuildings(new Building[]{building});
        return schoolZone;
    }

    public int addRoomType(Map map) {
        return roomDao.addRoomType(map);
    }

    public int delRoomType(long id) {
        return roomDao.delRoomType(id);
    }

    public int updateRoomType(Map map) {
        return roomDao.updateRoomType(map);
    }

    public List<Map<String,Object>> getRoomTypeList() {
        return roomDao.getRoomTypeList();
    }

    public List<Map<String,Object>> getMessages(int pageNum,int pageSize) {
//        System.out.println("test"+messageDao.selectMessage());
        int start = (pageNum-1)*pageSize;
        int end = start+pageSize;
        List<Map<String,Object>> message = messageDao.selectMessage();
        List<Map<String,Object>> isNotChecked = new ArrayList<>();
        List<Map<String,Object>> isChecked = new ArrayList<>();
        List<Map<String,Object>> result = new ArrayList<>();
        for(int i = 0;i<message.size();i++){
//            System.out.println(message.get(i).get("status"));
            if (message.get(i).get("status").equals(false)){
                isNotChecked.add(message.get(i));
            } else {
                isChecked.add(message.get(i));
            }
        }
        System.out.println("未读总数："+isNotChecked.size());
        System.out.println("已读总数："+isChecked.size());
        isNotChecked.addAll(isChecked);
        System.out.println("总数："+isNotChecked.size());
        System.out.println("所有消息"+isNotChecked);
        for (;start<end;start++){
            if (start>=isNotChecked.size())break;
            else result.add(isNotChecked.get(start));
        }
        System.out.println("结果数："+result.size());
        return result;
    }

    public int countMessages(){return messageDao.getTotalMessage();}

    public int delMessage(long[] ids) {
        if (ids.length==0) return 0;
        for (long id:ids
        ) {
            messageDao.deleteByPrimaryKey(id);
        }
        return 1;
    }

    public int modifyMessage(long id) {
        System.out.println(id);
        Message message = messageDao.selectByPrimaryKey(id);
        message.setStatus(1);
        return messageDao.updateByPrimaryKeySelective(message);
    }

    public Alarm[] alarms() { return alarmDao.selectAlarms(); }

    public int modifyAlarm(Alarm alarm) { return alarmDao.updateByPrimarySelective(alarm); }
}